/**
 * 
 */
package com.idea.zd.sys.controller;

import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.idea.zd.sys.model.Admin;
import com.idea.zd.sys.service.AdminService;
import com.idea.zd.sys.service.RoleService;
import com.idea.zd.sys.util.MD5Encrypt;
import com.idea.zd.sys.util.SysUtil;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.server.Service;

/**
 * @author y.z
 */
@ControllerBind(controllerKey="/admin",viewPath="/pages")
public class AdminController extends BaseController{
	private static final Logger log = Logger.getLogger(AdminController.class);
	@Service
	private AdminService adminService;
	@Service
	private RoleService roleService;
	private final String DEFAULT_PASS = "123456";
	
	/**
	 * 跳转到管理员列表
	 */
	@RequiresPermissions("admin/toAdmin")
	public void toAdmin(){
		getNavigation();
		render("admin/admin-list.html");
	}
	
	/**
	 * 跳转到管添加理员
	 */
	@RequiresPermissions("admin/toAddAdmin")
	public void toAddAdmin(){
		render("admin/admin-edit.html");
	}

	/**
	 * 跳转到管理员编辑
	 */
	@RequiresPermissions("admin/toEditAdmin")
	public void toEditAdmin(){
		render("admin/admin-edit.html");
	}
	
	/**
	 * 获取管理员列表
	 */
	public void getAdminList(){
		int curPage = getParaToInt("curPage");
		super.setPage(adminService.getPage(curPage));
	}
	
	/**
	 * 删除管理员
	 */

	@RequiresPermissions("admin/delAdmin")
	public void delAdmin(){
		String ids = getPara("ids");
		String idArray[] = ids.split(",");
		
		for(String id:idArray){
			adminService.delete(id);
		}
		
		msgSuccess();
	}
	
	
	/**
	 * 获取管理员类型
	 */
	public void getRoleList(){
		msgSuccess(roleService.getList());
	}
	
	/**
	 * 添加管理员
	 */
	public void addAdmin(){
		String headportrait = super.uploadImg("headportrait");
		String account = getPara("account");
		String name = getPara("name");
		String gender = getPara("gender");
		String phone = getPara("phone");
		String email = getPara("email");
		String type = getPara("type");
		String description = getPara("description");

		Admin admin = new Admin();
		admin.set("id", SysUtil.getUUID());
		admin.set("head_portrait",headportrait);
		admin.set("account",account );
		admin.set("name", name);
		admin.set("password", MD5Encrypt.MD5Encode(DEFAULT_PASS));
		admin.set("gender", gender);
		admin.set("phone", phone);
		admin.set("email", email);
		admin.set("type", type);
		admin.set("description",description);
		admin.set("createdate",new Date());
		
		if(adminService.add(admin)){
			msgAdd();
		}
		
	}
	
	/**
	 * 编辑管理员
	 */
	public void editAdmin(){
		String headportrait = super.uploadImg("headportrait");
		String id = getPara("id");
		String account = getPara("account");
		String name = getPara("name");
		String gender = getPara("gender");
		String phone = getPara("phone");
		String email = getPara("email");
		String type = getPara("type");
		String description = getPara("description");

		Admin admin = new Admin();
		admin.set("id", SysUtil.getUUID());
		
		if(!StrKit.isBlank(headportrait)){
			admin.set("head_portrait",headportrait);
		}
		
		admin.set("id",id );
		admin.set("account",account );
		admin.set("name", name);
		admin.set("gender", gender);
		admin.set("phone", phone);
		admin.set("email", email);
		admin.set("type", type);
		admin.set("description",description);
		
		if(adminService.update(admin)){
			msgUpdate();
		}
	}
	
	
	/**
	 * 修改密码
	 */
	public void editPass(){
		Admin admin = (Admin) getSession().getAttribute("admin");
		
		if(!admin.get("password").equals(MD5Encrypt.MD5Encode(this.getPara("pass")))){
			msgSetVal("请确认旧的密码");
			return;
		}
		
		if(this.getPara("repeatPass").equals(this.getPara("newPass"))){
			Admin admin2 = new Admin()
			.set("id", admin.get("id"))
			.set("password", MD5Encrypt.MD5Encode(this.getPara("newPass")));
			if(adminService.update(admin2)){
				msgSuccess();
			}else{
				msgSetVal("确认密码与新密码不符");
			}
		}
		
	}
	
	
	/**
	 * 密码重置
	 */
	@RequiresPermissions("admin/resetPass")
	public void resetPass(){
		Admin admin = new Admin()
		.set("id", this.getPara("id"))
		.set("password", MD5Encrypt.MD5Encode(DEFAULT_PASS));
		
		if(adminService.update(admin)){
			msgSuccess();
		}
	}
	
	/**
	 * 获取用户详情
	 */
	public void getAdminDetail(){
		Admin admin = adminService.getAdminById(getPara("id"));
		msgSuccess(admin);
	}
	
	/**
	 * 验证用户名是否存在
	 */
	public void validateAccount(){
		String id = getPara("id");
		String account = getPara("account");
		
		if(adminService.validateAccount(id, account)){
			super.msgSuccess();
		}else{
			super.msgSetVal("用户名已被占用");
		}
	}
}
